package org.sxf.app.sign.biz;

import android.content.Context;
import android.os.AsyncTask;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.util.LogUtils;

import org.sxf.app.sign.R;
import org.sxf.app.sign.constants.Constants;
import org.sxf.app.sign.data.DataCacheSingleton;
import org.sxf.app.sign.model.LogoutRepBean;
import org.sxf.app.sign.model.LogoutReqBean;
import org.sxf.app.sign.model.MsgRepBean;
import org.sxf.app.sign.model.SignRepBean;
import org.sxf.app.sign.model.SignReqBean;
import org.sxf.app.sign.provider.Client;
import org.sxf.app.sign.provider.LocProviderSingleton;
import org.sxf.app.sign.provider.MsgHandler;
import org.sxf.app.sign.utils.ConfigFileUtilsSingleton;
import org.sxf.app.sign.utils.TimeUtils;
import org.sxf.app.sign.utils.UIUtils;
import org.sxf.app.sign.utils.Utility;

import Ice.ConnectionRefusedException;
import Ice.SocketException;
import Ice.TimeoutException;
import Ice.UnknownException;

/**
 * Created by zqc on 17/1/17.
 */
public class SignBiz extends BaseBiz {

    public final static int BIZ_SIGN_FAIL = 0x131;
    public final static int BIZ_SIGN_SUCCESS = 0x132;

    public final static int ERROR_SIGN_NET_ERROR = 0xE31;


    private boolean isConnecting = false;

    public SignBiz(Context context, OnBizCallBackListener listener){
        this.mContext = context;
        this.mBizCallBackListener = listener;
    }

    private String createSignReqJson(){

        SignReqBean bean = new SignReqBean();
        bean.platform = Constants.PLATFORM;
        bean.corp_id = DataCacheSingleton.getInstance().getSchoolCode();
        bean.usr_id = DataCacheSingleton.getInstance().getStudentNo();
        bean.longitude = LocProviderSingleton.getInstance().getLon() + "";
        bean.latitude = LocProviderSingleton.getInstance().getLat() + "";
        bean.cli_time = TimeUtils.getNoneFormatTime();
        String reqStr = JSON.toJSONString(bean);
        LogUtils.d("msg:" + reqStr);
        return reqStr;
    }

    public void signApp(){

        if (isConnecting){
            return;
        }
        isConnecting = true;

        new AsyncTask<Void, Integer, String>() {
            @Override
            protected String doInBackground(Void... voids) {


                String reqStr = MsgHandler.msgEncode("00", createSignReqJson(), MsgHandler.NEED_TOKEN);

                String result = "";
                try {
                    LogUtils.d("send:" + reqStr);
                    result = Client.getServicePrx().signApp(reqStr);
                    LogUtils.d("result: " + result);
                }catch (ConnectionRefusedException e){
                    LogUtils.e(Utility.getStackTrace(e));
                }catch(TimeoutException e){
                    LogUtils.e(Utility.getStackTrace(e));
                }catch (SocketException e){
                    LogUtils.e(Utility.getStackTrace(e));
                }catch (UnknownException e){
                    LogUtils.e(Utility.getStackTrace(e));
                }catch (Exception e){
                    LogUtils.e(Utility.getStackTrace(e));
                }

                return result;
            }
            @Override
            protected void onPostExecute(String result){

                isConnecting = false;

                if (mIsQuit){
                    return;
                }
                try {

                    if (result == null || "".equals(result)){
                        safeOnBizError(ERROR_SIGN_NET_ERROR, null);
                        return;
                    }

                    MsgRepBean bean = checkMsg(result, true);
                    if (bean == null){
                        safeOnBizError(ERROR_SIGN_NET_ERROR, null);
                        return;
                    }

                    SignRepBean dataBean = JSON.parseObject(bean.rd, SignRepBean.class);
                    if (dataBean == null){
                        safeOnBizError(ERROR_SIGN_NET_ERROR, null);
                        return;
                    }

                    if (!dataBean.result.equals("0")){
                        int resultInt = Integer.parseInt(dataBean.result);
                        safeOnBizResult(BIZ_SIGN_FAIL, UIUtils.getResources().getStringArray(R.array.error_detail)[resultInt]);
                        return;
                    }

                    if (dataBean.corp_name == null || dataBean.srv_time == null){
                        safeOnBizError(ERROR_SIGN_NET_ERROR, null);
                        return;
                    }

                    safeOnBizResult(BIZ_SIGN_SUCCESS, dataBean);

                }catch (Exception e){
                    LogUtils.e(Utility.getStackTrace(e));
                    safeOnBizError(ERROR_SIGN_NET_ERROR, null);
                    return;
                }

                return;
            }
        }.execute();
    }


}
